﻿package{
	import flash.display.MovieClip;
	import flash.ui.Keyboard;
	import flash.events.*;
	import GameManager;
	import bullet;
	
	public class Character extends MovieClip{
		
		//Keys
		static public const LEFT:int = Keyboard.LEFT;
		static public const RIGHT:int = Keyboard.RIGHT;
		static public const DOWN:int = Keyboard.DOWN;
		static public const UP:int = Keyboard.UP;
		//wk5 fire button
		static public const FIRE:int = Keyboard.SPACE;

		
		//Movement Properties
		private var _power:int = 3;
		private var _leftPressed:Boolean = false;
		private var _rightPressed:Boolean = false;
		private var _upPressed:Boolean = false;
		private var _downPressed:Boolean = false;

		private var _NextX:int;
		private var _NextY:int;
		
		private var _GM:GameManager;
		
		public var isFacing:String;
		
		public function Character(){
			//listen for when the character is added to the play area
			addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event){
			//remove the added listener
			removeEventListener(Event.ADDED_TO_STAGE, onStage);
			
			_GM = parent as GameManager;
			
			// add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown,false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKUp,false,0,true);
			
			//register listeners for update stuff if we decouple the system more
			
			//when removed from stage clean up after your self
			addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
		}
		
		private function cleanUp(){
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
			//Remove Keyboard listeners
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKDown);
		}
		
		//START Input Management
		private function onKDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				_leftPressed = true;
				isFacing = "left";
				break;
				case RIGHT: 
				_rightPressed = true;
				isFacing = "right";
				break;
				case DOWN: 
				_downPressed = true;
				isFacing = "down";
				break;
				case UP: 
				_upPressed = true;
				isFacing = "up";
				break;

			}
			
		}
		
		private function onKUp(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				gotoAndPlay("start");
				_leftPressed = false;
				isFacing = "";
				break;
				case RIGHT: 
				gotoAndPlay("start");
				_rightPressed = false;
				isFacing = "";
				break;
				case DOWN: 
				_downPressed = false;
				isFacing = "";
				break;
				case UP: 
				_upPressed = false;
				isFacing = "";
				break;
				case FIRE:
				shootBullet();
				break;
			}
		}
		
		//changed from handleKeyInput
		public function nextPosition()
		{
			// set the _next position to the current position
			_NextX = x;
			_NextY = y;

			if(_leftPressed)
			{
				_NextX = x - _power;
				nextFrame();
				if(currentLabel != "left")
				{
					gotoAndStop("left");
				}

			}
			if(_rightPressed)
			{
				_NextX = x + _power;
				
				nextFrame();
				if(currentLabel != "right")
				{
					gotoAndStop("right");
				}
			}
			if(_upPressed)
			{
				_NextY = y - _power;
			}
			if(_downPressed)
			{
				_NextY = y + _power;
			}
			
		}
		//END Input Management
		
		public function checkCharacterCollision()
		{
			// Bottom Left - check clip 
			if(_GM.mazeClip.hitTestPoint(_NextX+11,_NextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(_GM.mazeClip.hitTestPoint(x+11,_NextY+28,true))
				{
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(_GM.mazeClip.hitTestPoint(_NextX+11,y+28,true))
				{
					_NextY = y;
				}
				
			}
			// Bottom Right
			if(_GM.mazeClip.hitTestPoint(_NextX+24,_NextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(_GM.mazeClip.hitTestPoint(x+24,_NextY+28,true))
				{
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(_GM.mazeClip.hitTestPoint(_NextX+24,y+28,true))
				{
					_NextY = y;
				}

			}
			// Top Left
			if(_GM.mazeClip.hitTestPoint(_NextX+11,_NextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(_GM.mazeClip.hitTestPoint(x+11,_NextY+4,true))
				{
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(_GM.mazeClip.hitTestPoint(_NextX+11,y+4,true))
				{
					_NextY = y;
				}
			}
			// Top Right
			if(_GM.mazeClip.hitTestPoint(_NextX+24,_NextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(_GM.mazeClip.hitTestPoint(x+24,_NextY+4,true))
				{
					_NextX = x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(_GM.mazeClip.hitTestPoint(_NextX+24,y+4,true))
				{
					_NextY = y;
				}
			}
			
			// check the fires
			// Bottom Left
			if(_GM.firesClip.hitTestPoint(_NextX+11,_NextY+28,true))
			{
				_NextX = 0;
				_NextY = 32;
			}
			// Bottom Right
			if(_GM.firesClip.hitTestPoint(_NextX+24,_NextY+28,true))
			{
				_NextX = 0;
				_NextY = 32;
			}
			// Top Left
			if(_GM.firesClip.hitTestPoint(_NextX+11,_NextY+4,true))
			{
				_NextX = 0;
				_NextY = 32;
			}
			// Top Right
			if(_GM.firesClip.hitTestPoint(_NextX+24,_NextY+4,true))
			{
				_NextX = 0;
				_NextY = 32;
			}
			
			// check exit
			// Bottom Left
			if(_GM.exitClip.hitTestPoint(_NextX+11,_NextY+28,true))
			{
				_GM.exitGame();
			}
			//Bottom Right
			if(_GM.exitClip.hitTestPoint(_NextX+24,_NextY+28,true))
			{
				_GM.exitGame();
			}
			// Top Left
			if(_GM.exitClip.hitTestPoint(_NextX+11,_NextY+4,true))
			{
				_GM.exitGame();
			}
			// Top Right
			if(_GM.exitClip.hitTestPoint(_NextX+24,_NextY+4,true))
			{
				_GM.exitGame();
			}

			//wk5
			// check the guards
			for each (var guard in _GM.guards){
				// Bottom Left
				if(guard.hitTestPoint(_NextX+11,_NextY+28,true))
				{
					_NextX = 0;
					_NextY = 32;
				}
				// Bottom Right
				if(guard.hitTestPoint(_NextX+24,_NextY+28,true))
				{
					_NextX = 0;
					_NextY = 32;
				}
				// Top Left
				if(guard.hitTestPoint(_NextX+11,_NextY+4,true))
				{
					_NextX = 0;
					_NextY = 32;
				}
				// Top Right
				if(guard.hitTestPoint(_NextX+24,_NextY+4,true))
				{
					_NextX = 0;
					_NextY = 32;
				}
			}
		}
		
		//
		public function makeMove()
		{
			if(_NextX>=0 && _NextX<=500)
			{
				x = _NextX;
			}
			if(_NextY>=0 && _NextY<=350)
			{
				y = _NextY;
			}
		}
		
		private function shootBullet(){
			var bulletDirection:String = "";
			
			if(_GM.bulletRef==null){
				bulletDirection = isFacing;
				//if the character is facing in a direction 
				if(bulletDirection!="")
				{
					//add bullet to the stage and register it in the bullet variable
					var aBullet:bullet = new bullet(bulletDirection,_GM.BULLETPOWER);
					_GM.bulletRef = aBullet;
					_GM.addChild(_GM.bulletRef);
					_GM.bulletRef.x = x + 16;
					_GM.bulletRef.y = y + 16;
				}
				
			}
		}

	}
}